/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_FrmTable.h"
#include "prs_FrmDefTemplate.h"
using namespace prs;

// TNodeFrmTable //////////////////////////////////////////////////////////////

const char *TNodeFrmTable::attrName_EnLinkToFrame = "enLinkToFrame";

//
frm::control::TTable *TNodeFrmTable::BuildControl(
  frm::control::TPanel *ParentControl
)
{
  frm::control::TTable *P = Control.GetValue();

  if (!P || &P->GetWindow() != &ParentControl->GetWindow()) {
    delete P; 
    P = new frm::control::TTable( ParentControl->GetWindow() );

    P->SetEnLinkToFrame(EnLinkToFrame);

    Control.SetValue(P);
    ParentControl->AddChildLast(P,false);

    Geometry.Value.UnionTo     (P->GetGeometry());
    Geometry.Value.UnionWidthTo(P->Grid->GetGeometry());

    Caption.Value.Control.SetValue(P->Caption);
    Grid   .Value.Control.SetValue(P->Grid);
  }
  
  return P;
}

// TRuleFrmTable //////////////////////////////////////////////////////////////

//
TRuleFrmTable::TRuleFrmTable(
  snt::text::TContext &Context,
  svl::TVariant &VParentNode,
  TRuleFrmChildrenBase &RChildren,
  TRuleSetNodeIdOptionally &RSetNodeIdOptionally,
  TRuleSetAttrValue &RSetAttrValue,
  TRuleFrmGeom &RFrmGeom,
  TRuleSetStyle &RStyle
) 
:vParentNode(VParentNode)
,rGeometry(RFrmGeom)
{
  snt::text::TSequence T(Context);
  snt::text::TKeyword K(Context);
  snt::TFVoid F;
  TFuncNewNodeIntoAttrOldChildren FNewNode;
  snt::TReturn Return;

  // Table ////////////////////////////////////
  this->Name(GetClassName()).Local(vParentNode)
  | K.New("table")
    & FNewNode.New(vParentNode,TNodeFrmTable())
    & RSetNodeIdOptionally.New()
    & RAttr.New()
    & Return.New()
  ;
  
  RAttr
  | T.New("(")
    & RAttr1.New()
    & F.New(this,&TRuleFrmTable::FSetGrid)
    & RChildren.New()
    & T.New(")")
    & Return.New()
  | Return.New()
  ;

  RAttr1
  | AAttr1.New()
  ;
  AAttr1
  | RStyle.New() 
    & AAttr1.New()

  | F.New(this,&TRuleFrmTable::FSetGeometryTable) 
    & RFrmGeom.New() 
    & AAttr1.New()
    
  | RSetAttrValue.New(
      VParentNode,
      TRuleSetAttrValue::TAttrs()
       .Add(TNodeFrmTable::attrName_EnLinkToFrame),
      RSetAttrValue.SetFuncValueIdent(this,&TRuleFrmTable::FSetParam)
    )
    & AAttr1.New()

  | RCaption.New() & Return.New()
  | Return.New()
  ;

  // Caption ////////////////
  RCaption.Local(vParentNode)
  | K.New("tableCaption") 
    & F.New(this,&TRuleFrmTable::FSetCaption)
    & RCaptionAttr.New()  
    & Return.New()
  ;

  RCaptionAttr
  | T.New("(")
    & RCaptionAttr1.New()
    & RChildren.New()
    & T.New(")")
    & Return.New()

  | Return.New()
  ;

  RCaptionAttr1
  | ACaptionAttr1.New()
  ;
  ACaptionAttr1
  | F.New(this,&TRuleFrmTable::FSetGeometryCaption)
    & RFrmGeom.New()
    & ACaptionAttr1.New()
    
  | Return.New()
  ;
}

//
bool TRuleFrmTable::FSetParam(TRuleSetAttrValue::TFuncValueIdentParams &Params) {
  
  //Params.Attr == TNodeFrmTable::attrName_EnLinkToFrame
  
  return TNodeDefTemplate::AddLinkAttrByParamId(Params.Attr,TFrmBoolValue(),Params.Ident,Params.OutError);
}
